<template>
  <el-card style="width: 500px;">
    <el-form label-width="80px" size="small" :model="form">
      <el-upload
              class="avatar-uploader"
              action="http://localhost:8000/file/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
      >
        <img v-if="form.aventUrl" :src="form.aventUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    <el-form-item label="用户名" ><el-input v-model="form.username" disabled autocomplete="off" placeholder="请输入用户名" style="width: 80%"></el-input></el-form-item>
    <el-form-item label="昵称" ><el-input v-model="form.nickname" autocomplete="off" placeholder="请输入昵称" style="width: 80%"></el-input></el-form-item>
    <el-form-item label="电话" ><el-input v-model="form.phone" autocomplete="off" placeholder="请输入电话" style="width: 80%"></el-input></el-form-item>
    <el-form-item label="邮箱" ><el-input v-model="form.email" autocomplete="off" placeholder="请输入邮箱" style="width: 80%"></el-input></el-form-item>
    <el-form-item label="地址" ><el-input v-model="form.address" type="textarea" autocomplete="off" placeholder="请输入地址" style="width: 80%"></el-input></el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</el-card>
</template>

<script>
export default {
  name: "Person",
  data(){
      return{
        form:{},
        user:localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.getUser().then(res => {
      this.form = res
    })
  },
  methods:{
    async getUser() {
      return (await this.request.get("/user/username/" + this.user.username)).data
    },
    save(){
      this.request.post("/user",this.form).then(res => {
        if (res){
          this.$message.success("操作成功")
          //触发更新父级更新user的方法
          this.$emit("refreshUser")
          // 更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })
        }else{
          this.$message.error("操作失败")
        }
      })
    },
    handleAvatarSuccess(res) {
      this.form.aventUrl = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      /*if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }*/
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<style >
    .avatar-uploader{
        text-align: center;
    }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-bottom: 10px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 137px;
    height: 137px;
    line-height: 138px;
    text-align: center;
  }
  .avatar {
    width: 138px;
    height: 138px;
    display: block;
  }
</style>